/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_base_light_spot.h
 * Desc:    Base light spot shader
 * Version: 1.08
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL/OpenGLES
*
\******************************************************************************/

#ifdef OPENGL || OPENGLES
	
	fixed3 color = float3(0.0f);
	
	float3 vertex = s_texcoord_1;
	
	fixed3 camera_direction = -normalize(vertex);
	
	fixed3 screen_normal = normalize(s_texcoord_2 * normal.x + s_texcoord_3 * normal.y + s_texcoord_4 * normal.z);
	
	#define LIGHT(NUMBER,SWIZZLE) { \
		half3 light_direction = (s_light_positions[NUMBER].xyz - vertex) * s_light_iradiuses. ## SWIZZLE; \
		half light_direction_length2 = dot(light_direction,light_direction); \
		half light_direction_ilength = rsqrt(light_direction_length2); \
		fixed light_attenuation = getAttenuation(s_light_attenuations. ## SWIZZLE - light_direction_length2); \
		fixed3 light = diffuse.xyz * (saturate(dot(light_direction,screen_normal) * light_direction_ilength) * s_material_shadings[NUMBER].x); \
		light += specular.xyz * (pow(saturate(dot(reflect(-light_direction,screen_normal),camera_direction) * light_direction_ilength),s_material_shadings[NUMBER].z) * s_material_shadings[NUMBER].y); \
		color += light * s_light_colors[NUMBER].xyz * light_attenuation; \
	}
	
	LIGHT(0,x)
	LIGHT(1,y)
	LIGHT(2,z)
	LIGHT(3,w)
	
/******************************************************************************\
*
* Direct3D11/Direct3D119
*
\******************************************************************************/

#elif DIRECT3D11 || DIRECT3D119
	
	half3 color = 0.0f;
	
	half3 vertex = IN.texcoord_1;
	
	half3 camera_direction = -normalize(vertex);
	
	half3 screen_normal = normalize(IN.texcoord_2 * normal.x + IN.texcoord_3 * normal.y + IN.texcoord_4 * normal.z);
	
	#define LIGHT(NUMBER,SWIZZLE) { \
		half3 light_direction = (s_light_positions[NUMBER].xyz - vertex) * s_light_iradiuses. ## SWIZZLE; \
		half light_direction_length2 = dot(light_direction,light_direction); \
		half light_direction_ilength = rsqrt(light_direction_length2); \
		half light_attenuation = getAttenuation(s_light_attenuations. ## SWIZZLE - light_direction_length2); \
		half3 light = diffuse.xyz * (saturate(dot(light_direction,screen_normal) * light_direction_ilength) * s_material_shadings[NUMBER].x); \
		light += specular.xyz * (pow(saturate(dot(reflect(-light_direction,screen_normal),camera_direction) * light_direction_ilength),s_material_shadings[NUMBER].z) * s_material_shadings[NUMBER].y); \
		color += light * s_light_colors[NUMBER].xyz * light_attenuation; \
	}
	
	LIGHT(0,x)
	LIGHT(1,y)
	LIGHT(2,z)
	LIGHT(3,w)
	
#endif
